<?php
/**
  * Вызывается в методе
  *
***/
require_once(X2 . '/classes/myDB/myDB.class.php');

$db =& myDB::getConnection2(
  "mysql://" . X2_SQL_USER .":". X2_SQL_PASS ."@". X2_SQL_HOST ."/". X2_SQL_DB
);
if(!$db) return false;
$db -> query("SET NAMES '".X2_SQL_DEFAULT_CHARSET."'");

$docId = intval($this -> doc -> get_attribute('id'));

$snippetId = isset($_REQUEST['article'])? $_REQUEST['article'] : 0;

$action = isset($_REQUEST['action'])? $_REQUEST['action'] : "";
$page = isset($_GET['page'])? intval($_GET['page']) : 0;
$offset = $page * 10;

switch($action){
  case 'edit':
  	if(!$this -> user -> isAuthorized()){
  	  echo "<p>Доступ запрещен.</p>";
  	  return;
  	}
	$data = array_merge(
	  array('subj' => '', 'description' => '', 'text' => '', 'comments' => 0),
	  $_POST
	);
  	if(isset($_POST['blog']) && $_POST['blog'] == 1){
  		// проверка ошибок
  		$errata = array();
  		if(count($errata) > 0){
  		  include(dirname(__FILE__) . "/tmpl/articleForm.php");
  		} else {
  			if($snippetId > 0){ //update
              $db -> query(
  		  	    "UPDATE `axe_snippet` SET
  		  	      subj='".$db -> quote($_POST['subj'])."',
  		  	      description='".$db -> quote($_POST['description'])."',
  		  	      text='".$db -> quote($_POST['text'])."',
  		  	      comments=".(isset($_POST['comments'])? 1 : 0)."
  		  	      WHERE id={$snippetId}"
  		      );
//  		      var_export($db); exit;
  			} else { //insert
  		      $db -> query(
  		  	    "INSERT INTO `axe_snippet` (docId, subj, description, text, tstamp, comments)
  		  	    VALUES (
  		  	      ".intval($_POST['docId']).",
  		  	      '".$db -> quote($_POST['subj'])."',
  		  	      '".$db -> quote($_POST['description'])."',
  		  	      '".$db -> quote($_POST['text'])."',
  		  	      ". time() .",
  		  	      ".(isset($_POST['comments'])? 1: 0)."
  		  	    )"
  		      );
  		      $snippetId = $db -> lastId();
  		    }
            header("Location: ". $_SERVER['REDIRECT_URL']);
            exit;
  		}
  	} else {
  	  if($snippetId > 0){
  	    $data = $db -> arrayQuery(
          "SELECT * FROM `axe_snippet` WHERE id={$snippetId}"
        );
        $data = ($data)? $data[0] : array('subj' => '', 'description' => '', 'text' => '');
  	  }
      include(dirname(__FILE__) . "/tmpl/articleForm.php");
    }
  break;
  case 'delete':
  	if(!$this -> user -> isAuthorized()){
  	  echo "<p>Доступ запрещен.</p>";
  	  return;
  	}
  	// заметка
  	if($this -> user-> userInfo['remove'] == 1 && isset($_GET['article']) && intval($_GET['article']) > 0){
  	  $db -> query(
  	    "DELETE FROM `axe_snippet` WHERE id=". intval($_GET['article'])
  	  );
  	  $db -> query(
  	  	"DELETE FROM `axe_comments` WHERE docId=". intval($_GET['article'])
  	  );
      header("Location: ". $_SERVER['REDIRECT_URL'] .($page > 0 ? "?page=". $page : ''));
      exit;
    }
    // камент
  	if($this -> user-> userInfo['moderate'] == 1 && isset($_GET['comment']) && intval($_GET['comment']) > 0){
  	  $db -> query(
  	    "DELETE FROM `axe_comments` WHERE id=". intval($_GET['comment'])
  	  );
      header("Location: ". $_GET['redir']);
      exit;
    }
  break;
  default:
  if(count($_POST) > 0 && isset($_POST['comments']) && $_POST['comments'] = 1){
    $docId = intval($_POST['docId']);
    include(dirname(__FILE__) . "/comments.php");
  }
  if($snippetId > 0){
    // выводим сниппет
    $data = $db -> arrayQuery(
      "SELECT * FROM `axe_snippet` WHERE id={$snippetId}"
    );
    if($data){
      include(dirname(__FILE__) . "/tmpl/article.php");
      if($data[0]['comments'] == 1){
        // Каменты к
        $docId = $this -> doc -> get_attribute('id') * 10000 + $snippetId;
        include(dirname(__FILE__) . "/comments.php");
      }
    }

  } else {
    // последние заметки и пейджер, если нужно
    $data = $db -> arrayQuery(
      "SELECT * FROM `axe_snippet`
      ORDER BY tstamp DESC
      LIMIT {$offset}, 10",
      "id"
    );
    if($data){
      foreach($data as $id => $v){
?>
<div class="art">
  <div class="date"><?php echo humanDate($v['tstamp']) ?></div>
  <h3><a href="<?php echo $_SERVER['REDIRECT_URL'] ?>?article=<?php echo $id ?>"><?php echo htmlspecialchars($v['subj'], ENT_NOQUOTES) ?></a><?php
if($this -> user -> authorized && $this -> user -> userInfo['update'] == 1){
?>
<a href="<?php echo $_SERVER['REDIRECT_URL'] ?>?action=edit&amp;article=<?php echo $id ?>"><img src="/skin/i/edit.gif" border="0" title="редактировать" hspace="6" /></a>
<?php
}
if($this -> user -> authorized && $this -> user -> userInfo['remove'] == 1){
?>
<a href="<?php echo $_SERVER['REDIRECT_URL'] ?>?action=delete&amp;article=<?php echo $id ?>" onclick="return confirm('Вы уверены, что хотите удалить документ?')"><img src="/skin/i/delete.gif" border="0" title="удалить" hspace="6" /></a>
<?php
}
?></h3>
<div class="description">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td><?php echo $v['description'] ?></td>
</tr>
</table></div>
<?php
if($v['comments'] == 1 && $this -> user -> userInfo['comment']){
?>
<div class="comments">
<?php
  $comId = $this -> doc -> get_attribute('id') * 10000 + $id;
  $entryCommentsCount = $db -> getSingle(
    "SELECT COUNT(*) AS c FROM `axe_comments` WHERE docId=$comId",
    "c"
  );
?>
<a href="<?php echo $_SERVER['REDIRECT_URL'] ?>?article=<?php echo $id ?>">
<?php
  if($entryCommentsCount == 0){
    echo "Комментировать";
  } else {
    echo "Комментариев: ". $entryCommentsCount;
  }
?>
</a></div>
<?php
}
?>
</div>
<?php
      }// WHERE id={$docId}
    $count = $db -> getSingle(
      "SELECT COUNT(*) AS c FROM `axe_snippet`",
      "c"
    );

    pager($_SERVER['REDIRECT_URL'], $count, $page+1);
    }
  }
}
?>